//
//  UIViewController+GNUtil.m
//  MarketEleven
//
//  Created by Bergren Lam on 8/7/14.
//  Copyright (c) 2014 Meinekechina. All rights reserved.
//

#import "UIViewController+GNUtil.h"

#import "Appearance.h"
#import <objc/runtime.h>
#import "UIColor+GNUtil.h"

const char * emptyTypeKey = "emptyType";
const char * emptyCustomImgKey = "emptyCustomImg";
const char * emptyCustomTitleKey = "emptyCustomTitle";


@implementation UIViewController (GNUtil)

+ (UIViewController *)viewControllerWithStoryboard:(NSString *)storyboard identifier:(NSString *)identify
{
    NSString *name = storyboard;
    if (!name) name = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UIMainStoryboardFile"];
    
    if (!name) name = @"MainStoryboard";
    
    UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:name bundle:nil];
    return  [stryBoard instantiateViewControllerWithIdentifier:identify];
}

+ (UIViewController *)currentViewController{
    UIViewController *VC = [[UIApplication sharedApplication] delegate].window.rootViewController;
    if ([VC isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)VC;
        UINavigationController *nav = (UINavigationController *)tabBarController.selectedViewController;
        UIViewController * baseVC = (UIViewController *)nav.visibleViewController;
        return baseVC;
    } else{
        return VC;
    }
}

#pragma mark 空白页相关

#pragma mark setter && getter
- (void)setEmptyType:(LKEmptyType)emptyType {
    NSNumber *tempEmptyType = @(emptyType);
    objc_setAssociatedObject(self,emptyTypeKey, tempEmptyType, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (LKEmptyType)emptyType {
    NSNumber *tempEmptyType = objc_getAssociatedObject(self, emptyTypeKey);
    return tempEmptyType.integerValue;
}

- (void)setEmptyCustomImg:(NSString *)emptyCustomImg {
    objc_setAssociatedObject(self,emptyCustomImgKey, emptyCustomImg, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)emptyCustomImg {
    return objc_getAssociatedObject(self, emptyCustomImgKey);
}

- (void)setEmptyCustomTitle:(NSString *)emptyCustomTitle {
    objc_setAssociatedObject(self,emptyCustomTitleKey, emptyCustomTitle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)emptyCustomTitle {
    return objc_getAssociatedObject(self, emptyCustomTitleKey);
}

#pragma 空白页代理
// 返回标题文字
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    self.view.backgroundColor = [UIColor clearColor];
    NSString *text = @"暂无数据";
//    if (![CJSUserManger isExistenceNetwork]) {
//        //没网
//        text = @"请检查网络设置";
//    }else
    if (self.emptyType == LKEmptyTypeForNoData){
        if (self.emptyCustomTitle) {
            text = self.emptyCustomTitle;
        }else {
            text = @"暂无数据";
        }
    }else if (self.emptyType == LKEmptyTypeForFailure){
        text = @"数据请求失败，请稍后再试";
    }else if (self.emptyType == LKEmptyTypeForSuccess){
        text = @"";
    }else if (self.emptyType == LKEmptyTypeForMathNotBegain){
        text = @"比赛未开始";
    }
    NSString *color = @"666666";
    if (self.emptyCustomImg) {
        color = @"FFFFFF";
    }
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor colorWithHexString:color]};
    return [[NSAttributedString alloc] initWithString:text attributes:attribute];
}

// 空白区域点击事件
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (self.emptyType == LKEmptyTypeForFailure || self.emptyType == LKEmptyTypeForNoData) {
        [self emptyDidSelected:scrollView];
    }
    NSLog(@"---:空白页点击");
}

// 返回图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
//    if (![CJSUserManger isExistenceNetwork]) {
//        //没网
//        return [UIImage imageNamed:@"暂无事件数据"];
//    }else
        if (self.emptyType == LKEmptyTypeForNoData){
        //没数据，自定义
        if (self.emptyCustomImg) {
            return [UIImage imageNamed:self.emptyCustomImg];
        }
    }else if (self.emptyType == LKEmptyTypeForFailure){
        return [UIImage imageNamed:@"暂无事件数据"];
    }else if (self.emptyType == LKEmptyTypeForSuccess){
        return [UIImage imageNamed:@""];
    }else if (self.emptyType == LKEmptyTypeForMathNotBegain){
        return [UIImage imageNamed:@"quiz_未开赛"];
    }
    //没数据
    return [UIImage imageNamed:@"暂无事件数据"];
}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    self.view.backgroundColor = [UIColor clearColor];
    if (self.emptyType != LKEmptyTypeForLoding) {
        return nil;
    }
    UIImageView* mainImageView= [[UIImageView alloc] init];
    mainImageView.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"loading_0"],
                                     [UIImage imageNamed:@"loading_1"],
                                     [UIImage imageNamed:@"loading_2"],
                                     [UIImage imageNamed:@"loading_3"],
                                     [UIImage imageNamed:@"loading_4"],
                                     [UIImage imageNamed:@"loading_5"],
                                     [UIImage imageNamed:@"loading_6"],
                                     [UIImage imageNamed:@"loading_7"],
                                     [UIImage imageNamed:@"loading_8"],
                                     [UIImage imageNamed:@"loading_9"],nil];
    [mainImageView setContentMode:UIViewContentModeCenter];
    [mainImageView setAnimationDuration:0.2f];
    [mainImageView setAnimationRepeatCount:0];
    [mainImageView startAnimating];
    return mainImageView;
}

- (void)emptyDidSelected:(UIScrollView *)scrollView {
    
    
}

@end
